<?php
/**
 * Created by PhpStorm.
 * User: dolia
 * Date: 2016/4/6
 * Time: 15:50
 */

namespace App\Http\Controllers\Api\V1;


use App\Exceptions\Api\ApiDoneErrorException;
use App\Exceptions\Api\ApiInputErrorException;
use App\Exceptions\Api\ApiRsaEecryptErrorException;
use App\Exceptions\Api\ApiUserNotLoginErrorException;
use App\Exceptions\Api\ApiValidateErrorException;
use App\Http\Controllers\Controller;
use App\Interfaces\GoodsService;
use App\Interfaces\ShopService;
use App\Interfaces\UserCollectionService;
use App\Http\Requests\Api\ApiRequest as Request;

class ShopController extends Controller{

    /**
     * @api {post} /api/shop 店铺页
     * @apiName Shop/index
     * @apiGroup Shop
     * @apiDescription 店铺页显示
     * @apiParam {int} shop_id 店铺ID
     * @apiParam {int} [page] 第几页
     * @apiSuccess {Object} Object 店铺
     * @apiSuccess {Object} Object.shop 店铺信息
     * @apiSuccess {int} Object.shop.id 店铺ID
     * @apiSuccess {string} Object.shop.shop_name 店铺名称
     * @apiSuccess {string} Object.shop.shop_logo 店铺LOGO
     * @apiSuccess {string} Object.shop.shop_desc 店铺描述
     * @apiSuccess {int} Object.shop.sort_order 排序
     * @apiSuccess {int} Object.shop.is_show 是否显示
     * @apiSuccess {string} Object.shop.mobile_shop_logo 店铺LOGO地址
     * @apiSuccess {int} Object.shopCollectionStatus 店铺收藏状态
     * @apiSuccess {object} Object.goods 商品分页信息
     * @apiSuccess {object[]}  Object.goods.data   商品信息
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
    {
         "shop": {
            "id": 4,
            "shop_name": "麦德龙",
            "shop_logo": "ea0fab52e56980636d45258318029918",
            "shop_desc": "麦德龙",
            "sort_order": 0,
            "is_show": 1,
            "mobile_shop_logo": "http://dolia.img-cn-hangzhou.aliyuncs.com/ea0fab52e56980636d45258318029918.png"
         },
        "shopCollectionStatus": 0,
        "goods": {
        "total": 1050,
        "per_page": 15,
        "current_page": 1,
        "last_page": 70,
        "next_page_url": "http://www.afuyigou.cn/api/shop?page=2",
        "prev_page_url": null,
        "from": 1,
        "to": 15,
        "data": [
        {
        "goods_id": 105,
        "cat_id": 417,
        "goods_sn": "af000098",
        "goods_bar_code": "8806154843171",
        "goods_name": "贝尊Babyprime 杰奎琳小米枕头 粉色",
        "goods_pet_name": "用心关爱 给宝宝最大的关怀",
        "goods_desc": "枕头套100%棉/填充物100%小米，降低宝宝的体温，确保宝宝好睡眠。",
        "brand_id": 63,
        "goods_number": 10,
        "goods_weight": "0.000",
        "market_price": "158.00",
        "shop_price": "106.00",
        "keywords": "枕头,婴儿枕头",
        "goods_content": "",
        "goods_thumb": "c1222e02c470b7b6a7b3c4c312646a1d",
        "resource": "韩国",
        "type": 1,
        "goods_gallery_id": 0,
        "is_on_sale": 1,
        "add_time": 0,
        "sort_order": 100,
        "is_delete": 0,
        "is_home_recommend": 0,
        "is_cat_recommend": 0,
        "shop_id": 5,
        "sales_num": 0,
        "deleted_at": null,
        "search_keywords": "枕头,婴儿枕头,贝尊 Babyprime,母婴用品,母婴用品,宝宝用品,宝宝,婴儿,宝宝家居,宝宝,家居",
        "created_at": "2016-01-15 12:00:00",
        "updated_at": "2016-03-22 09:38:58",
        "audit_status": 1,
        "mobile_goods_thumb": "http://dolia.img-cn-hangzhou.aliyuncs.com/c1222e02c470b7b6a7b3c4c312646a1d.jpg"
        }
     ]
    }
    }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400 Bad Request
    {
    "message": "缺少请求参数goods_id"
    }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 202 Accepted
    {
    "message": "该店铺不存在"
    }
     */
    public function index(Request $request,GoodsService $goodsService,ShopService $shopService,UserCollectionService $collectionService){
        $shop_id = $request->get('shop_id');
        $page = $request->get('page');
        if(empty($shop_id)){
            throw new ApiInputErrorException('缺少请求参数goods_id');
        }
        $shopCollectionStatus = 0;
        $user = $request->apiUser->get();
        if($user){
            $user_id = $request->apiUser->get()->id;
            $userCollection = $collectionService->isInCollection($user_id,"SHOP",$shop_id);
            if($userCollection){
                $shopCollectionStatus = 1;
            }
        }
        $data = [];
        $goods = $goodsService->getGoodsByShop($shop_id,$page)->toArray();
        $shop = $shopService->get($shop_id);

        $data['shop']=$shop;
        $data['shopCollectionStatus']=$shopCollectionStatus;
        $data['goods']=$goods;
        if(empty($data['shop'])){
            throw new ApiDoneErrorException('该店铺不存在');
        }
        return response()->json($data);

    }

} 